package io.github.maojian.queue;

/**
 *  622. 设计循环队列
 *
 *  数组实现
 */
public class Q622_1 {
    private int front;

    private int rear;

    private int capacity;

    private int size;

    private int[] elements;

    //设置队列长度为空
    public Q622_1(int k) {
        capacity = k + 1;
        elements = new int[capacity];
        rear = front = 0;
    }

    //向循环队列插入一个元素，若插入成功，则返回true
    public boolean enQueue(int value) {
        if (isFull()) return false;
        elements[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
    }

    //从循环队列删除一个元素，删除成功返回true
    public boolean deQueue() {
        if (isEmpty()) return false;
        front = (front + 1) % capacity;
        return true;
    }

    //获取队首元素，队列为空，返回-1
    public int Front() {
        if (isEmpty()) return -1;
        return elements[front];
    }

    //获取队尾元素，队列为空，返回-1
    public int Rear() {
        if (isEmpty()) return -1;
        return elements[(rear - 1 + capacity) % capacity];
    }

    //检查循环队列是否为空
    public boolean isEmpty() {
        return front == rear;
    }

    //检查循环队列是否已满
    public boolean isFull() {
        return ((rear + 1) % capacity) == front;
    }
}
